package com.yz.rabbitmqspringboot.cumsumer;

import com.rabbitmq.client.Channel;
import com.yz.rabbitmqspringboot.pojo.Order;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Component
public class MyListener {

//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = "springboot-topic-queue",durable = "true"),
//            exchange = @Exchange(value ="springboot-topic-exchange",durable = "true",type = ExchangeTypes.TOPIC
//                    ,ignoreDeclarationExceptions = "true"),
//            key = "springboot.*"
//    ))
//    @RabbitListener(queues = "${topic.queue.name}")
//    @RabbitHandler
//    public void onMessage(Message message, Channel channel) throws IOException {
//        System.err.println("消费端 payload:"+ message.getPayload());
//        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
//        channel.basicAck(deliveryTag, false);
//    }


//    @RabbitListener(queues = "${topic.queue.name}")
//    @RabbitHandler
//    public void onMessage2(@Payload Order order, @Headers Map<String,Object> headers, Channel channel) throws IOException {
//        System.err.println("消费端 payload:"+ order);
//        Long deliveryTag = (Long)headers.get(AmqpHeaders.DELIVERY_TAG);
//        channel.basicAck(deliveryTag, false);
//    }
    //自定义 concurrency containerFactory
    //@RabbitListener(queues = "${topic.queue.name}",containerFactory = "myConsumerlistenerContainer",concurrency = "5-10")
    @RabbitListener(queues = "${topic.queue.name}")
    @RabbitHandler
    public void onMessageLimit(@Payload Order order, @Headers Map<String,Object> headers, Channel channel) throws IOException, InterruptedException {
        System.err.println("消费端 payload:"+ order);
        Long deliveryTag = (Long)headers.get(AmqpHeaders.DELIVERY_TAG);
        channel.basicAck(deliveryTag, false);
        TimeUnit.SECONDS.sleep(5);
    }
}
